package mainmodel

import (
	"strconv"
	"sync"
	"web_server/model"
)

var userNum *int64 = nil
var userNumMut sync.RWMutex

func GetUserModel(id int64) *UserModel {
	var user *UserModel = new(UserModel)
	if err := model.DBSlaveRand().Get(user, "SELECT id, namecode, username, role FROM t_base_user where id = ?", id); err != nil {
		// panic(err)
		return nil
	}
	return user
}

func GetUserNum() int64 {
	userNumMut.RLock()
	if userNum != nil {
		userNumMut.RUnlock()
		return *userNum
	}
	userNumMut.RUnlock()
	userNumMut.Lock()
	defer userNumMut.Unlock()
	if userNum != nil {
		return *userNum
	}
	userNum = new(int64)
	if err := model.DBSlaveRand().Get(userNum, "SELECT count(*) FROM t_base_user"); err != nil {
		panic(err)
		// return nil
	}
	return *userNum
}

type userElemList struct {
	Id       int64
	Namecode string
	Username string
	Role     string
}

func GetUserList(pagenum int64) []userElemList {
	var umlist []UserModel
	if err := model.DBSlaveRand().Select(&umlist, "SELECT id, namecode, username, role FROM t_base_user LIMIT 20 OFFSET ?", (pagenum-1)*20); err != nil {
		panic(err)
	}
	rolemap := GetRoleMap()
	// fmt.Println("umlist ::: ", umlist)
	userlist := make([]userElemList, len(umlist))
	for i, v := range umlist {
		userlist[i] = userElemList{
			Id:       v.Id,
			Namecode: v.Namecode,
			Username: v.Username,
			Role:     rolemap[v.Role],
		}
	}
	return userlist
}

func GetUserListByCode(namecode string) []userElemList {
	var umlist []UserModel
	if err := model.DBSlaveRand().Select(&umlist, "SELECT id, namecode, username, role FROM t_base_user WHERE namecode = ?", namecode); err != nil {
		panic(err)
	}
	rolemap := GetRoleMap()
	userlist := make([]userElemList, len(umlist))
	for i, v := range umlist {
		userlist[i] = userElemList{
			Id:       v.Id,
			Namecode: v.Namecode,
			Username: v.Username,
			Role:     rolemap[v.Role],
		}
	}
	return userlist
}

type UserElemAdd struct {
	Namecode string `json:"namecode"`
	Username string `json:"username"`
	Password string `json:"password"`
	Role     int    `json:"role"`
}

func SetUserAdd(ue UserElemAdd) error {
	_, err := model.DBMaster().Exec("INSERT INTO t_base_user (namecode, username, password, role) VALUES (?, ?, ?, ?)", ue.Namecode, ue.Username, encodePassword(ue.Password), ue.Role)
	return err
}

type UserElemEdit struct {
	Id       int    `json:"id"`
	Namecode string `json:"namecode"`
	Username string `json:"username"`
	Password string `json:"password"`
	Role     int    `json:"role"`
}

func SetUserEdit(ue UserElemEdit) error {
	if len(ue.Password) != 0 {
		_, err := model.DBMaster().Exec("UPDATE t_base_user SET namecode = ?, username = ?, password = ?, role = ? WHERE id = ?", ue.Namecode, ue.Username, encodePassword(ue.Password), ue.Role, ue.Id)
		return err
	} else {
		_, err := model.DBMaster().Exec("UPDATE t_base_user SET namecode = ?, username = ?, role = ? WHERE id = ?", ue.Namecode, ue.Username, ue.Role, ue.Id)
		return err
	}
}

func SetUserDel(ids []int) error {
	idsStr := strconv.Itoa(ids[0])
	for i := 1; i < len(ids); i++ {
		idsStr += ","
		idsStr += strconv.Itoa(ids[i])
	}
	_, err := model.DBMaster().Exec("DELETE FROM t_base_user WHERE id IN(" + idsStr + ")")
	return err
}

func SetUserPsw(psw string, id int64) error {
	_, err := model.DBMaster().Exec("UPDATE t_base_user SET password = ? WHERE id = ?", encodePassword(psw), id)
	return err
}
